using Firestarter.DB.Schema.BO;
using Firestarter.DB.Schema.Test.BO.TestUtils;
using FireStarterModeller.BO;
using FireStarterModeller.Test;
using Habanero.BO;
using NUnit.Framework;

namespace Firestarter.DB.Schema.Test.BO
{
    /// <summary>
    /// Provides a place to write custom tests for DBColumn objects.
    /// This file is only written once and can be changed.  The Def file
    /// attached to this as a dependent is rewritten with each regeneration
    /// and contains the standard tests for DBColumn.
    /// Regenerate this test project whenever there have been changes to the
    /// business objects.
    /// If tests are failing due to a unique setup in your application,
    /// you can either override the Create methods in TestUtils, or you
    /// can add the test to the ignore list below and reimplement it here.
    /// </summary>
    public partial class TestDBColumn
    {
        [TestFixtureSetUp]
        public void SetupTestFixture()
        {
            //------------------------------------------------------------
            // Use this list to ignore generated tests that are failing
            // due to a unique condition in your application.
            // Remember to reimplement the test here.
            //------------------------------------------------------------
            //_ignoreList.Add("TestMethodName", "Reason for ignoring it");
            base.SetupFixture();
        }

        [Test]
        public void Test_ToString()
        {
            //---------------Set up test pack-------------------
            DBColumn column = TestUtilsDBColumn.CreateUnsavedValidDBColumn();
            //---------------Assert Precondition----------------
            Assert.IsNotNull(column.ColumnName);
            Assert.IsNotNull(column.Table);
            Assert.IsNotNull(column.Table.TableName);
            //---------------Execute Test ----------------------
            string actualToString = column.ToString();
            //---------------Test Result -----------------------
            Assert.AreEqual(column.Table.TableName + " - " + column.ColumnName, actualToString);
        }

        [Test]
        public void Test_ToString_TableIsNull()
        {
            //---------------Set up test pack-------------------
            DBColumn column = new DBColumn("ColumnName");
            //---------------Assert Precondition----------------
            Assert.IsNotNull(column.ColumnName);
            Assert.IsNull(column.Table);
            //---------------Execute Test ----------------------
            string actualToString = column.ToString();
            //---------------Test Result -----------------------
            Assert.AreEqual(" - " + column.ColumnName, actualToString);
        }

        [Test]
        public void Test_SetMappedDMProperty()
        {
            //---------------Set up test pack-------------------
            CheckIfTestShouldBeIgnored();
            //---------------Set up test pack-------------------
            BORegistry.DataAccessor = new DataAccessorInMemory();
            DBColumn dBColumn = TestUtilsDBColumn.CreateSavedDBColumn();
            DMProperty boForRelationshipMappedDMProperty = TestUtilsDMProperty.CreateSavedDMProperty();

            //---------------Assert Precondition ---------------
            Assert.AreNotEqual(dBColumn.ColumnID, boForRelationshipMappedDMProperty.MappedDBColumnID);
            //---------------Execute Test ----------------------
            dBColumn.SetMappedDMProperty(boForRelationshipMappedDMProperty);
            //---------------Test Result -----------------------
            Assert.IsNotNull(boForRelationshipMappedDMProperty.MappedDBColumnID);
            Assert.AreEqual(dBColumn.ColumnID, boForRelationshipMappedDMProperty.MappedDBColumnID);
            Assert.IsNotNull(dBColumn.GetMappedDMProperty());
        }

        [Test]
        public void Test_SetMappedDMProperty_Saved()
        {
            //---------------Set up test pack-------------------
            CheckIfTestShouldBeIgnored();
            //---------------Set up test pack-------------------
            BORegistry.DataAccessor = new DataAccessorInMemory();
            DBColumn dBColumn = TestUtilsDBColumn.CreateSavedDBColumn();
            DMProperty boForRelationshipMappedDMProperty = TestUtilsDMProperty.CreateSavedDMProperty();
            //---------------Assert Precondition ---------------
            Assert.AreNotEqual(dBColumn.ColumnID, boForRelationshipMappedDMProperty.MappedDBColumnID);
            //---------------Execute Test ----------------------
            dBColumn.SetMappedDMProperty(boForRelationshipMappedDMProperty);
            dBColumn.Save();
            //---------------Test Result -----------------------
            Assert.IsNotNull(boForRelationshipMappedDMProperty.MappedDBColumnID);
            Assert.AreEqual(dBColumn.ColumnID, boForRelationshipMappedDMProperty.MappedDBColumnID);
            Assert.IsNotNull(dBColumn.GetMappedDMProperty());
        }

        [Test]
        public void Test_MappedPropertyName_WhenColumnNull_ShouldRetEmptyString()
        {
            //---------------Set up test pack-------------------
            var dbColumn = new DBColumn();
            //---------------Assert Precondition----------------
            Assert.IsNotNull(dbColumn);
            Assert.IsNull(dbColumn.GetMappedDMProperty());
            //---------------Execute Test ----------------------
            string propertyName = dbColumn.GetMappedPropertyName();
            //---------------Test Result -----------------------
            Assert.IsEmpty(propertyName);
        }

        [Test]
        public void Test_MappedPropertyName_WhenColumnNotNull_ShouldRetDBPropertyName()
        {
            //---------------Set up test pack-------------------
            var dbColumn = new DBColumn();
            string expectedPropertyName = GetRandomString();
            var mappedDMProperty = new DMProperty();
            mappedDMProperty.PropertyName = expectedPropertyName;
            dbColumn.SetMappedDMProperty(mappedDMProperty);
            //---------------Assert Precondition----------------
            Assert.IsNotNull(dbColumn);
            Assert.IsNotNull(dbColumn.GetMappedDMProperty());
            Assert.IsNotNullOrEmpty(dbColumn.GetMappedDMProperty().PropertyName);
            //---------------Execute Test ----------------------
            string propertyName = dbColumn.GetMappedPropertyName();
            //---------------Test Result -----------------------
            Assert.AreEqual(expectedPropertyName, propertyName);
        }

        [Test]
        public void Test_TableName_WhenColumnNull_ShouldRetEmptyString()
        {
            //---------------Set up test pack-------------------
            var dbColumn = new DBColumn();
            //---------------Assert Precondition----------------
            Assert.IsNotNull(dbColumn);
            Assert.IsNull(dbColumn.Table);
            //---------------Execute Test ----------------------
            string tableName = dbColumn.TableName ;
            //---------------Test Result -----------------------
            Assert.IsEmpty(tableName);
        }

        [Test]
        public void Test_TableName_WhenColumnNotNull_ShouldRetDBTableName()
        {
            //---------------Set up test pack-------------------
            var dbColumn = new DBColumn();
            string expectedTableName = GetRandomString();
            var mappedDBTable = new DBTable {TableName = expectedTableName};
            dbColumn.Table = mappedDBTable;
            //---------------Assert Precondition----------------
            Assert.IsNotNull(dbColumn);
            Assert.IsNotNull(dbColumn.Table);
            Assert.IsNotNullOrEmpty(dbColumn.Table.TableName);
            //---------------Execute Test ----------------------
            string tableName = dbColumn.TableName;
            //---------------Test Result -----------------------
            Assert.AreEqual(expectedTableName, tableName);
        }
        private static string GetRandomString()
        {
            return TestUtilsShared.GetRandomString();
        }
    }
}